package com.ruoyi.oar.api.util;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.sign.Base64;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.SecureRandom;

/**
 * @author saw
 * @date 2021年11月25 16:49
 * @description
 */
public class EncryptUtil {

    public static final byte[] MD5(String str) {
        try {
            byte[] res = str.getBytes(StandardCharsets.UTF_8);
            MessageDigest mdTemp = MessageDigest.getInstance("MD5".toUpperCase());
            mdTemp.update(res);
            byte[] hash = mdTemp.digest();
            return hash;
        } catch (Exception var4) {
            return null;
        }
    }

    public static String JM(byte[] inStr) {
        String newStr = new String(inStr);
        char[] a = newStr.toCharArray();

        for (int i = 0; i < a.length; ++i) {
            a[i] = (char) (a[i] ^ 116);
        }

        String k = new String(a);
        return k;
    }

    public static String BASE64Encrypt(byte[] key) {
        String edata = null;

        try {
            edata = Base64.encode(key);
        } catch (Exception var3) {
            var3.printStackTrace();
        }

        return edata.replaceAll("\r|\n", "");
    }

    public static byte[] BASE64Decrypt(String data) {
        if (data == null) {
            return null;
        } else {
            Object var1 = null;

            try {
                byte[] edata = Base64.decode(data);
                return edata;
            } catch (Exception var3) {
                var3.printStackTrace();
                return null;
            }
        }
    }

    public static byte[] DES3Encrypt(String key, String str) {
        byte[] newkey = key.getBytes();
        SecureRandom sr = new SecureRandom();
        byte[] bt = null;
        try {
            DESedeKeySpec dks = new DESedeKeySpec(newkey);
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
            SecretKey securekey = keyFactory.generateSecret(dks);
            Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
            cipher.init(1, securekey, sr);
            bt = cipher.doFinal(str.getBytes(StandardCharsets.UTF_8));
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }

        return bt;
    }

    public static String DES3Decrypt(byte[] edata, String key) {
        String data = "";

        try {
            if (edata != null) {
                byte[] newkey = key.getBytes();
                DESedeKeySpec dks = new DESedeKeySpec(newkey);
                SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
                SecretKey securekey = keyFactory.generateSecret(dks);
                Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
                cipher.init(2, securekey, new SecureRandom());
                byte[] bb = cipher.doFinal(edata);
                data = new String(bb, "UTF-8");
            }
        } catch (Exception var9) {
            var9.printStackTrace();
        }

        return data;
    }

    public static String AESEncode(String encodeRules, String content) throws Exception {
        KeyGenerator keygen = KeyGenerator.getInstance("AES");
        keygen.init(128, new SecureRandom(encodeRules.getBytes()));
        SecretKey original_key = keygen.generateKey();
        byte[] raw = original_key.getEncoded();
        SecretKey key = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(1, key);
        byte[] byte_encode = content.getBytes("utf-8");
        byte[] byte_AES = cipher.doFinal(byte_encode);
        String AES_encode = Base64.encode(byte_AES);
        return AES_encode;
    }

    public static String AESDecode(String encodeRules, String content) throws Exception {
        KeyGenerator keygen = KeyGenerator.getInstance("AES");
        keygen.init(128, new SecureRandom(encodeRules.getBytes()));
        SecretKey original_key = keygen.generateKey();
        byte[] raw = original_key.getEncoded();
        SecretKey key = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(2, key);
        byte[] byte_content = Base64.decode(content);
        byte[] byte_decode = cipher.doFinal(byte_content);
        String AES_decode = new String(byte_decode, "utf-8");
        return AES_decode;
    }
}

